package com.example.springworkadmin.controller;

import com.example.springworkadmin.dao.ResidentAccountDao;
import com.example.springworkadmin.dao.ResidentStatusDao;
import com.example.springworkadmin.entity.DTO.AfterResponseDTO;
import com.example.springworkadmin.entity.ResidentAccount;
import com.example.springworkadmin.entity.ResidentStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/residentHealthy")
public class ResidentHealthyController {
    @Autowired
    ResidentStatusDao residentStatusDao;
    @Autowired
    ResidentAccountDao residentAccountDao;

    @GetMapping("/getPage")
    public String getPage(){
        return "dataManage/residentHealthy/residentHealthyList";
    }

    @GetMapping("/lookPage")
    public String lookPage(){
        return "dataManage/residentHealthy/lookResidentHealthy";
    }

    /**
     * 根据关键字获取居民健康数据（后台）
     * @param pages 当前页数
     * @param limit 页面大小
     * @param key 关键字
     * @return 资源列表list、资源总数count
     */
    @RequestMapping("/get")
    @ResponseBody
    public AfterResponseDTO get(@RequestParam(value = "page",defaultValue = "1")Integer pages,
                                @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                @RequestParam(value = "key",defaultValue = "")String key){
        int start = (pages-1)*limit;
        List<ResidentAccount> list=residentAccountDao.get(start,limit,key);
        int count=residentAccountDao.count(key);
        return new AfterResponseDTO(list,count,0);
    }

    /**
     * 根据关键字获取居民健康数据（后台）
     * @param id 当前主键
     * @param key 关键字
     * @return 资源列表list、资源总数count
     */
    @RequestMapping("/getById")
    @ResponseBody
    public AfterResponseDTO getById(@RequestParam(value = "id",defaultValue = "0")String id,
                                    @RequestParam(value = "key",defaultValue = "2000/01/01 00:00:00 - 3000/01/01 00:00:00")String key){
        String[] strArray=key.split("-");
        String startTime=strArray[0].trim();
        String endTime=strArray[1].trim();
        List<ResidentStatus> list=residentStatusDao.get(id,startTime,endTime);
        int count=residentStatusDao.count(id,startTime,endTime);
        return new AfterResponseDTO(list,count,0);
    }
}
